<template>
  <el-container>
    <el-container>
      <el-main>
        <el-card class="box-card search-form">
          <el-form ref="form" :model="search" :inline="true">
            <el-form-item label="表单名称">
              <el-input v-model="search.title"></el-input>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" @click="onSearch" size="small"
                >查询</el-button
              >
            </el-form-item>
          </el-form>
          <el-button
            type="primary"
            @click="add"
            size="small"
            icon="el-icon-plus"
            >新增表单</el-button
          >
        </el-card>
        <el-card class="box-card" style="margin-top: 10px">
          <el-table :data="tableData" style="width: 100%; margin-bottom: 20px">
            <el-table-column label="名称" prop="title"></el-table-column>
            <el-table-column label="表名" prop="table"></el-table-column>
            <el-table-column
              label="当前版本"
              prop="now_version"
            ></el-table-column>
            <el-table-column
              label="部署版本"
              prop="deploy_version"
            ></el-table-column>
            <el-table-column label="状态" prop="status">
              <template #default="scope">
                <span v-if="scope.row.status == 2">
                  <el-tag size="medium" type="success">已部署</el-tag>
                </span>
                <span v-else
                  ><el-tag size="medium" type="danger">未部署</el-tag></span
                >
              </template>
            </el-table-column>
            <el-table-column label="操作" fixed="right" width="250">
              <template #default="scope">
                <el-button
                  type="text"
                  size="small"
                  @click="table_edit(scope.row, scope.$index)"
                  icon="el-icon-edit"
                  v-if="scope.row.status == 1"
                  >编辑</el-button
                >
                <el-divider
                  direction="vertical"
                  v-if="scope.row.status == 1"
                ></el-divider>
                <el-button
                  type="text"
                  size="small"
                  @click="table_design(scope.row, scope.$index)"
                  icon="el-icon-setting"
                  v-if="scope.row.status == 1"
                  >设计</el-button
                >
                <el-divider
                  direction="vertical"
                  v-if="scope.row.status == 1"
                ></el-divider>
                <el-button
                  type="text"
                  size="small"
                  @click="table_deploy(scope.row, scope.$index)"
                  icon="el-icon-cloudy"
                  v-if="scope.row.status == 1"
                  >部署</el-button
                >
                <el-button
                  type="text"
                  size="small"
                  @click="table_undeploy(scope.row, scope.$index)"
                  icon="el-icon-cloudy"
                  v-else
                  >卸载</el-button
                >
                <el-divider direction="vertical" v-if="scope.row.status == 1"
                  >></el-divider
                >
                <el-popconfirm
                  title="确定删除吗？"
                  @confirm="table_del(scope.row, scope.$index)"
                  v-if="scope.row.status == 1"
                >
                  <template #reference>
                    <el-button type="text" size="small" icon="el-icon-delete"
                      >删除</el-button
                    >
                  </template>
                </el-popconfirm>
              </template>
            </el-table-column>
          </el-table>
          <div class="page">
            <el-pagination
              :hide-on-single-page="page.total < page.size ? true : false"
              background
              :current-page="page.currentPage"
              :page-sizes="page.pageSizes"
              :page-size="page.size"
              layout="total, sizes, prev, pager, next, jumper"
              :total="page.total"
              @size-change="handleSizeChange"
              @current-change="handleCurrentChange"
            >
            </el-pagination>
          </div>
        </el-card>
      </el-main>
    </el-container>

    <save-dialog
      v-if="dialog.save"
      ref="saveDialog"
      @success="handleSaveSuccess"
      @closed="dialog.save = false"
    ></save-dialog>
    <designer
      v-if="dialog.designer"
      ref="designerDialog"
      @success="handleSaveSuccess"
      @closed="closedDesigner"
    ></designer>
  </el-container>
</template>

<script>
import saveDialog from "./save";
import designer from "./designer";

export default {
  name: "design-form",
  components: {
    saveDialog,
    designer,
  },
  data() {
    return {
      dialog: {
        save: false,
        designer: false,
      },
      tableData: [],
      page: {
        total: 0,
        currentPage: 1,
        size: 15,
        pageSizes: [15, 25, 35, 45],
      },
      search: {
        title: "",
        page: 1,
        pageSize: 15,
      },
    };
  },
  mounted() {
    this.getList();
  },
  methods: {
    add() {
      this.dialog.save = true;
      this.$nextTick(() => {
        this.$refs.saveDialog.open("add");
      });
    },
    closedDesigner() {
      this.dialog.designer = false;
      this.getList();
    },
    onSearch() {
      this.getList();
    },
    table_edit(row) {
      this.dialog.save = true;
      this.$nextTick(() => {
        this.$refs.saveDialog.open("edit").setData(row);
      });
    },
    async table_deploy(row) {
      this.$confirm("您想如何部署?", "提示", {
        confirmButtonText: "增量部署",
        cancelButtonText: "覆盖部署",
        type: "warning",
      })
        .then(async () => {
          let res = await this.$api.form.deploy.post({
            id: row.id,
            type: 2,
          });

          if (res.code == 0) {
            this.$message.success(res.msg);
            this.getList();
          } else {
            this.$message.error(res.msg);
          }
        })
        .catch(async () => {
          let res = await this.$api.form.deploy.post({
            id: row.id,
            type: 1,
          });

          if (res.code == 0) {
            this.$message.success(res.msg);
            this.getList();
          } else {
            this.$message.error(res.msg);
          }
        });
    },
    table_design(row) {
      this.dialog.designer = true;
      this.$nextTick(() => {
        this.$refs.designerDialog.open().setData(row);
      });
    },
    table_undeploy(row) {
      this.$confirm("卸载的同时要抹除数据吗?", "提示", {
        confirmButtonText: "抹除",
        cancelButtonText: "保留",
        type: "warning",
      })
        .then(async () => {
          let res = await this.$api.form.undeploy.get({
            id: row.id,
            type: 1,
          });

          if (res.code == 0) {
            this.$message.success(res.msg);
            this.getList();
          } else {
            this.$message.error(res.msg);
          }
        })
        .catch(async () => {
          let res = await this.$api.form.undeploy.get({
            id: row.id,
            type: 2,
          });

          if (res.code == 0) {
            this.$message.success(res.msg);
            this.getList();
          } else {
            this.$message.error(res.msg);
          }
        });
    },
    async table_del(row) {
      let res = await this.$api.form.del.get({ id: row.id });
      if (res.code == 0) {
        this.$message.success(res.msg);
        this.getList();
      } else {
        this.$message.error(res.msg);
      }
    },
    async getList() {
      let res = await this.$api.form.list.get(this.search);
      if (res.code == 0) {
        this.tableData = res.data.rows;
        this.page.total = res.data.total;
      } else {
        this.$message.error(res.msg);
      }
    },
    // 分页规格改变
    handleSizeChange(val) {
      this.page.size = val;
      this.page.currentPage = 1;
      this.search.page = 1;
      this.search.limit = val;
      this.getList();
    },
    // 分页点击
    handleCurrentChange(val) {
      this.page.currentPage = val;
      this.search.page = val;
      this.search.limit = this.page.size;
      this.getList();
    },
    handleSaveSuccess() {
      this.getList();
    },
  },
};
</script>

<style>
.list-title {
  display: inline-block;
  color: #17233d;
  font-weight: 500;
  font-size: 20px;
  border-left: 5px solid #409eff;
  padding-left: 20px;
}
</style>
